package com.lanhai.mind.impl;

import com.lanhai.mind.beans.AgvBean; 
import com.lanhai.mind.interfaces.IAgvEvent;
import com.lanhai.mind.utils.DataSendTools;

/**
 * 小车指令。如果没有协议上的变化 ，可不用修改
 * @author zhengwei
 *
 */
public class AgvEventImpl implements IAgvEvent {
 

	@Override
	public void toRoad(AgvBean agv, int road) {
		byte[] buf = new byte[8];
		buf[0] = (byte) 0xA1;
		buf[1] = (byte) agv.getAgvId();
		buf[2] = 1;
		buf[3] = (byte) road;

		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		buf[7] = s;
		DataSendTools.sendData(agv.getIp(), agv.getPort(), buf);
	}

	@Override
	public void stopOrstart(AgvBean agv, boolean isStop) {
		byte[] buf = new byte[8];
		buf[0] = (byte) 0xA1;
		buf[1] = (byte) agv.getAgvId();;
		buf[2] = 3;
		if (isStop) {
			buf[5] = 1;
		} else {
			buf[5] = 2;
		}

		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		buf[7] = s;
		DataSendTools.sendData(agv.getIp(), agv.getPort(), buf);
	}

	@Override
	public void transUp(AgvBean agv, boolean isReceive) {
		byte[] buf = new byte[8];
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;
		
		if(isReceive) {
			//上层传入
			buf[4]=3;
		}else {
			//上层传出
			buf[4]=2;
		}
		
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		buf[7] = s; 
		DataSendTools.sendData(agv.getIp(), agv.getPort(), buf);
	}

	@Override
	public void transDown(AgvBean agv, boolean isReceive) {
		byte[] buf = new byte[8];
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;
		
		if(isReceive) {
			//下层传入
			buf[4]=5;
		}else {
			//下层传出
			buf[4]=4;
		}
		
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		buf[7] = s; 
		DataSendTools.sendData(agv.getIp(), agv.getPort(), buf);
	}

	@Override
	public void transUpOutAndDownIn(AgvBean agv) {
		byte[] buf = new byte[8];
		buf[0] = (byte) 0xA1;
		buf[1] = (byte)  agv.getAgvId();
		buf[2] = 2;
		

		 buf[4]=6;
		
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		buf[7] = s;
		DataSendTools.sendData(agv.getIp(), agv.getPort(), buf);
	}

	@Override
	public void transUpInAndDownOut(AgvBean agv) {
		byte[] buf = new byte[8];
		buf[0] = (byte) 0xA1;
		buf[1] = (byte) agv.getAgvId();
		buf[2] = 2;
		 
		 buf[4]=7;
		 
		
		byte s = 0;
		for (int i = 0; i < 7; i++) {
			s += buf[i];
		}
		buf[7] = s;
		DataSendTools.sendData(agv.getIp(), agv.getPort(), buf);
	}

	@Override
	public void releaseAllResource(AgvBean agv) {
		// TODO Auto-generated method stub
		
	}
	
	
}
